var caseBasicInfoId,caselength=0;
var caseData;//案件信息
var linkNameId,caseStep,sort=0;//当前环节信息，顺序
var nextLinkNameId,nextCaseStep,nextsort=0,nextRequired;//下一环节信息，顺序,是否必要环节
var nextLinkNameId_,nextCaseStep_,nextsort_=0,nextRequired_; //存储与nextCaseStep相邻的的下一环节
var sort_click=0;//被点击的环节顺序
/*online=false; 
$(document).ready(function(){
	findCaseBasicInfoByIdOffLine("7",initCaseHandle);
});*/

/**
 * 初始化案件办理
 */
function initCaseHandle(row){
	if(row.caseStatus == "申请撤销"){
		$("#nextFlow_save_link").hide();
	}
	$("#revoke_save_link").hide();
	$("#handle").layout();
	$("#tjhj").combobox();
	findLinkNameByOid(organId,'1',setInitLinkName);
	findLinkNameByOid(organId,'0',initTjhj);
	$("#handle_").layout();
	caseData = row;
	caseBasicInfoId = row.id;
	caseStep = row.caseStep;
	if(caseStep=="结案"){
		$("#docUpLoad").empty();
	}
	nextCaseStep = row.nextCaseStep;
	$("#uploadDoc").linkbutton({
		width:70,
		height:26
	});
	
	 /**
     * 初始化文书类型
     */
	if(online){
		$("#doctypes").show();
	}else{
		$("#doctypes").hide();
		$("#docUpLoad").hide();
	}
	loadHtml("../register/wsPrint.html","#wsdy");
}

/**
 * 初始化环节
 * @param data
 */
function initLinkName(data){
	requiredCaseStep = data;
	caselength = data.length;
	$.each(data,function(index,item){
		$("#accordion").append("<div id='" + item.linkNameId + "' rqed='"+item.required+"' linkName='"+item.linkName+"' sort='"+index+"' title='" + item.linkName + "' data-options=\"iconCls:'icon-sys'\"><ul idul='" + item.linkNameId + "'></ul></div>");
		findDocByLinkNameId(item.linkNameId,organId,initDocName);
		initTooltip(item);
	});
	//$("#accordion").accordion({border:false,selected:''});
	$("#accordion").accordion({border:false,selected:'',onAdd:function(title,index){
		var linkNameId = $("div[linkName="+title+"]").attr("id");
		findDocByLinkNameId(linkNameId,organId,initDocName);
	}});
	
	/**
	 * 案件下一环节
	 */
	//alert($("#accordion div[linkName='" + nextCaseStep + "']").attr('sort'));
	$("#accordion div[linkName='" + nextCaseStep + "']").each(function(index,item){
		nextsort = parseInt($(item).attr("sort"));
		if(nextsort < caselength-1){
			nextsort_ = nextsort + 1;
		}else{
			nextsort_ = nextsort;
		}
		nextLinkNameId = $("#accordion div[sort='"+nextsort+"']").attr("id");
		nextRequired = $("#accordion #"+ nextLinkNameId).attr('rqed');
		$('#nextFlow_text').text($("#accordion #"+ nextLinkNameId).attr("linkName"));
	});
/*	nextsort = parseInt($("#accordion div[linkName='" + nextCaseStep + "']").attr("sort"));
	alert(nextsort);
	nextLinkNameId = $("#accordion div[sort='"+nextsort+"']").attr("id");
	nextRequired = $("#accordion div[sort='"+nextsort+"']").attr('rqed');
	$('#nextFlow_text').text($("#accordion div[sort='"+nextsort+"']").attr("linkName"));*/
	$("#accordion").accordion('select',$("#accordion div[sort="+nextsort+"]").attr('linkName'));
}

/**
 * 查询案件非必要环节，将其添加至案件的必要环节
 */
function setInitLinkName(data){
	if(online){
		findCaseBasicInfoClByIdAndRequiredOnLine(caseBasicInfoId,'0',initLinkName,data);
	}else{
		findCaseBasicInfoClByIdAndRequiredOffLine(caseBasicInfoId,'0',initLinkName,data);
	}
}

/**
 * 查询案件的在线非必要环节
 * @param data
 */
function findCaseBasicInfoClByIdAndRequiredOnLine(id,required,callback,data){
	$.ajax({
		type : "GET",
		url : getContextPath() + "/caseBasicInfoCl/findByCaseIdAndReqiured",
		dataType : "json",
		data : {
			caseId:id,required:required
		},
		success : function(tempdata) {
		    if(tempdata.result=='success'){
		    	var tempCaseStep = setCaseStep(data,tempdata.content);
		    	/*var tempCaseStep=new Array();
		    	$.each(data,function(index,item){
			    	$.each(tempdata.content,function(index_,item_){
		    			if(item.linkName==item_.nextCaseStep){
					    	$.each(noRequiredCaseStep,function(index1_,item1_){
				    			if(item_.caseStep == item1_.linkName){
				    				tempCaseStep.push(item1_);
				    			}
					    	});
		    			}
			    	});
		    		
			    	tempCaseStep.push(item);

			    	if(item.linkName == caseStep){
				    	$.each(noRequiredCaseStep,function(index_,item_){
			    			if(item_.linkName == nextCaseStep && (jQuery.inArray( item_, tempCaseStep )==-1 )){
			    				tempCaseStep.push(item_);
			    			}
				    	});
			    	}
		    	});*/
		    	
		    	if(typeof(callback)=='function'){
		    		callback(tempCaseStep);
		    	}
		    }else if(tempdata.result=='fail'){
		    	message(data.errorCode + ":" +data.errorMessage);
		    }
		}
	 });
}

function setCaseStep(data,tempdata){
	var tempCaseStep=new Array();
	$.each(data,function(index,item){
    	$.each(tempdata,function(index_,item_){
			if(item.linkName==item_.nextCaseStep){
		    	$.each(noRequiredCaseStep,function(index1_,item1_){
	    			if(item_.caseStep == item1_.linkName){
	    				tempCaseStep.push(item1_);
	    			}
		    	});
			}
    	});
		
    	tempCaseStep.push(item);

    	if(item.linkName == caseStep){
	    	$.each(noRequiredCaseStep,function(index_,item_){
    			if(item_.linkName == nextCaseStep && (jQuery.inArray( item_, tempCaseStep )==-1 )){
    				tempCaseStep.push(item_);
    			}
	    	});
    	}
	});
	return tempCaseStep;
}

/**
 * 初始化环节
 * @param data
 */
function initTjhj(data){
	noRequiredCaseStep = data;
	$("#tjhj").combobox({
		valueField: 'linkNameId',
		textField: 'linkName',
		width:100,
		data:data,
		editable:false,
		onSelect:function(item){
			/*$("#accordion").accordion('getSelected').parent('.panel').before(
					'<div class="panel" style="width: 195px;"><div class="panel-header accordion-header accordion-header-selected" style="height: 16px; width: 185px;"><div class="panel-title panel-with-icon">立案审批</div><div class="panel-icon icon-sys"></div><div class="panel-tool"><a class="panel-tool-collapse" href="javascript:void(0)" style="display: none;"></a><a href="javascript:void(0)" class="accordion-collapse"></a></div></div><div id="1000000300000000" linkname="立案审批" sort="1" title="" data-options="iconCls:\'icon-sys\'" class="panel-body accordion-body" style="display: block; width: 195px;"><ul idul="1000000300000000"><li><span style="cursor:pointer" linkname="立案审批" linknameid="1000000300000000" docpath="fillingApproveInput.html" docnameid="1001000300000000" onclick="openDoc(\'1001000300000000\')"><span class="icon icon-nav" style="cursor:pointer">&nbsp;</span><span class="nav">立案审批表</span></span></li></ul></div></div>'
			);*/
			if(caseData.caseStep == caseData.nextCaseStep){
				message("该案件已结案");
				$("#tjhj").combobox('setValue','');
				return;
			}
			/**
			 * 一个必要环节之前只能添加一个非必要环节
			 * 一个非必要环节只能添加一次
			 * 非必要环节只能添加在必要环节之前
			 */
			var tempsort = parseInt($("#accordion div[linkName='" + nextCaseStep + "']").attr("sort"))-1;
			var temprqed = $("#accordion div[linkName='" + nextCaseStep + "']").attr('rqed');
			if(temprqed == 0){
				message("非必要环节只能添加在必要环节之前");
				$("#tjhj").combobox('setValue','');
				return;
			}
			temprqed = $("#accordion div[sort='"+tempsort+"']").attr("rqed");
			if(temprqed == '0'){
				message("该必要环节之前已存在非必要环节");
				$("#tjhj").combobox('setValue','');
				return;
			}
			/*$("#accordion div[linkName='" + nextCaseStep + "']").each(function(index,item){
				var tempsort = parseInt($(item).attr('sort'))-1;
				var temprqed = $(item).attr('rqed');
				if(temprqed == 0){
					message("非必要环节只能添加在必要环节之前");
					return;
				}
				temprqed = $("#accordion div[sort='"+tempsort+"']").attr("rqed");
				if(temprqed == '0'){
					message("该必要环节之前已存在非必要环节");
					return;
				}
			});*/
			var tempLinkName = $("#accordion #" + item.linkNameId);
			if(tempLinkName.length!=0){
				message(item.linkName + "环节已存在");
				$("#tjhj").combobox('setValue','');
				return;
			}
			$.messager.confirm('提示', '确定要添加吗?', function(bool){
				if (bool){
					if(online){
						$.ajax({
							type : "POST",
							url : getContextPath() + "/caseBasicInfo/updateCaseStep",
							dataType : "json",
							data : {
								id:caseBasicInfoId,linkName:item.linkName,userId:userId,username:userName
							},
							success : function(data) {
							    if(data.result=='success'){
							    	resetCaseStep(item);
							    }else if(data.result=='fail'){
							    	message(data.errorCode + ":" +data.errorMessage);
							    }
							}
						 });
					}else{
						updateCaseBasicInfoCaseeStepOffLine_(caseBasicInfoId,item.linkName,item);
					}
				}
			});
			$("#tjhj").combobox('setValue','');
			
			//$("#accordion").removeClass().removeAttr("data-options").accordion();
			//$("#accordion #"+nextLinkNameId).parent('.panel').before("<div id='" + item.linkNameId + "' required='"+item.required+"' linkName='"+item.linkName+"' sort='"+nextsort+"' title='" + item.linkName + "' data-options=\"iconCls:'icon-sys'\"><ul idul='" + item.linkNameId + "'></ul></div>");
/*			$('#accordion').accordion('add', {
				iconCls:'icon-sys',
				title: item.linkName,
				content:"<div id='" + item.linkNameId + "' linkName='"+item.linkName+"' title='" + item.linkName + "'><ul idul='" + item.linkNameId + "'></ul></div>",
				selected: false
			});*/
		}
	});
}
/**
 * 添加动态环节后，重新初始化右侧环节菜单
 * @param item
 */
function resetCaseStep(item){
	$("#ajhj #accordion").remove();
	$("#ajhj").append("<div class=\"easyui-accordion\" id=\"accordion\" data-options=\"border:false\"></div>");
	//caseData.nextCaseStep=caseData.caseStep;
	//caseData.caseStep=item.linkName;
	caseData.nextCaseStep=item.linkName;
	nextCaseStep=item.linkName;
	var caseBasicInfoCaseStep= new Array();
	$.each(requiredCaseStep,function(index_,item_){
		if(nextLinkNameId==item_.linkNameId){
			caseBasicInfoCaseStep.push(item);
			caseBasicInfoCaseStep.push(item_);
		}else{
			caseBasicInfoCaseStep.push(item_);
		}
	});
	initLinkName(caseBasicInfoCaseStep);
}

/**
 * 初始化环节包含的文书
 * @param data
 */
function initDocName(data){
	$.each(data,function(index,item){
		var gxhDocPath_= $.trim(item.gxhDocPath).length == 0 ?'':item.gxhDocPath;
		var tswsbs_ = $.trim(item.tsInput).length == 0 ?'':item.tsInput;
		$("#accordion div ul[idul=" + item.linkNameId+"]").append(
		 "<li>" +
         "<span style='cursor:pointer' linkName='"+item.linkName+"' linkNameId='"+item.linkNameId+"' docPath='"+item.docPath+"' docNameId='"+item.docNameId+"' tsInput='"+tswsbs_+"' gxhDocPath='"+gxhDocPath_+"' onclick=openDoc('"+item.docNameId+"')><span class='icon icon-nav' style='cursor:pointer'>&nbsp;</span><span class='nav'>" + item.docName + "</span></span>"+
         "</li>");
	});
	$("#accordion div[sort="+nextsort+"] span:first").eq(0).trigger('click');
}


/**
 * 案件环节提示信息
 * @param data
 * @param linkNameId
 */
function tooltip(data,linkNameId){
	var content = "";
	$.each(data,function(index,item){
		content += 
			'<div style="padding:5px 10px">环节名称:' + item.caseStep + '</div>' + 
			'<div style="padding:5px 10px">办理人员:' + item.createName +'</div>' +
			'<div style="padding:5px 10px">办理时间:' + item.createTime +'</div>' +
			'<div style="padding:5px 10px">执  法 人:' + item.staff1 + ',' + item.staff2 +'</div>';
	});
	$("#accordion #" + linkNameId).tooltip({
        content: content,
        position:'right'
    });
}

/**
 * 环节处理信息
 * @param item
 */
function initTooltip(item){
	if(online){
		findCaseBasicInfoClByIdOnLine(caseBasicInfoId,item.linkNameId,item.linkName,tooltip);
	}else{
		findCaseBasicInfoClByIdOffLine(caseBasicInfoId,item.linkNameId,item.linkName,tooltip);
	}
}

/**
 * 打开文书
 * @param linkNameId
 */
function openDoc(docNameId){
	var item = {};
	var span = $("#accordion span[docNameId="+docNameId+"]");
	$("#accordion ul li").removeClass("li-selected");
	$(span).parent("li").addClass("li-selected");
	
	item.linkNameId = $(span).attr("linkNameId");
	var div = $("#accordion div[id="+item.linkNameId+"]");
	var click_sort = parseInt($(div).attr('sort'));
	if(click_sort>nextsort){
		message('请办理上一环节');
		return;
	}
	$('#nextFlow_text').text($(span).attr("linkName"));
	item.linkName = $(span).attr("linkName");
	item.docPath = $(span).attr("docPath");
	item.gxhDocPath = $(span).attr("gxhDocPath");
	item.tsInput = $(span).attr("tsInput");
	var docPath = item.docPath.substring(0,item.docPath.indexOf("Input."));
	var docPathLen = item.docPath.length;
	var first = docPath.substring(0,1).toUpperCase();
	var last = docPath.substring(1,docPathLen);
	var func;

	if(online){
		func = "find" + first + last + "ByIdOnLine";
	}else{
		func = "find" + first + last + "ByIdOffLine";
	}
	if(nextsort > click_sort || caseData.caseStep==caseData.nextCaseStep){
		$('#nextFlow_text').text(item.linkName);
		var pathDetail = $.trim(item.gxhDocPath).length == 0 ? "../detail/" : "../"+item.gxhDocPath+"/detail/";
		$("#docNames").load(pathDetail + docPath + "Detail.html",function(){
			if($.trim(item.tsInput).length > 0){
				var funcStr = "set"+ first + last + "Data";
				window[func](caseBasicInfoId,window[funcStr]);
			}
//			if(docPath=="sampleEvidence"){
//				window[func](caseBasicInfoId,setSampleEvidenceData);
//			}
//			if(docPath=="entrustIdentify"){
//				window[func](caseBasicInfoId,setEntrustIdentifyData);
//			}
//			if(docPath=="evidenceList"){
//				window[func](caseBasicInfoId,setEvidenceListData);
//			}
//			if(docPath=="caseProcessIdea"){
//				window[func](caseBasicInfoId,setCaseProcessIdeaData);
//			}
//			if(docPath=="documentReceipt"){
//				window[func](caseBasicInfoId,setDocumentReceiptData);
//			}
//			if(docPath=="inquiryNotes"){
//				window[func](caseBasicInfoId,setInquiryNotesData);
//			}
//			if(docPath=="punishementDecision"){
//				window[func](caseBasicInfoId,setPunishementDecisionData);
//			}
//			if(docPath=="fillingApprove"){
//				window[func](caseBasicInfoId,setFillingApproveData);
//			}
//			if(docPath=="fileList"){
//				window[func](caseBasicInfoId,setFileListData);
//			}
//			if(docPath=="spotPunishmentDecision"){
//				window[func](caseBasicInfoId,setSpotPunishmentDecisionData);
//			}
//			if(docPath=="punishmentFinalReport"){
//				window[func](caseBasicInfoId,setPunishmentFinalReportData);
//			}
//			if(docPath=="withholdCertificate"){
//				window[func](caseBasicInfoId,setWithholdCertificateData);
//			}
			else
			{
				window[func](caseBasicInfoId,setData);
			}
			$("#revoke_save_link").hide();
			$("#doctypes").hide();
			$("#uploadDoc").hide();
			
		});
	}else{
		$('#nextFlow_text').text(item.linkName);
			var pathInput = $.trim(item.gxhDocPath).length == 0 ? "../register/" : "../"+item.gxhDocPath+"/register/";
			$("#docNames").load(pathInput + item.docPath,function(){
				caseData.id = '';
				window[docPath](caseData);
				if(online){
					$("#uploadDoc").show();
					$("#doctypes").show();
				}
			});
			if(item.linkNameId == "1000000300000000"){
				$("#revoke_save_link").show();
			}else{
				$("#revoke_save_link").hide();
			}
		//同步文书类型
		if(online){
		  // queryTemplateDetailByDocId(docNameId,docPath,setCaseeTemplateDetail,"#wslx");
		   queryTemplateByDocId(docNameId,docPath,setCaseeTemplateDetail,"#mbmc");
		}
	}
	
   
}

function revoke(){
	$('#revokeDialog').dialog({
		title : '填写申请撤销理由',
		width : 600,
		height : 300,
		left : 300,
		top : 50,
		closed : false,
		cache : false,
		modal : true,
		href : "../register/revoke.html",
		onLoad : function() {
			
		},
		buttons : [{
			text : '提交',
			handler : function() {
				var time = currentTime();
				var revoke = $("#revokeForm").serializeJson();
				revoke.caseStatus = "申请撤销";
				if($.trim(revoke.id).length == 0){
					/**
					 * 添加
					 */
					revoke.createId = userId;
					revoke.createName = userName;
					revoke.createTime = time;
				}else{
					/**
					 * 修改
					 */
					revoke.modifyId = userId;
					revoke.modifyTime = time;
				}
				saveRevoke(revoke,setDataId);
				$("#nextFlow_save_link").hide();
			}
		},
		{
			text : '取消',
			handler : function() {
				$('#revokeDialog').dialog('close');
			}
		}]
	});
}

function saveRevoke(revoke,callback){
	$.ajax({
		type : "POST",
		url : ctx+ "/revoke/create",
		data : revoke,
		dataType : "json",
		success : function(data) {
			if(typeof(callback) == 'function'){
				callback(data.content,"#revokeForm");
			}
			if($.trim(revoke.id).length == 0){
				message("申请撤销提交成功");
			}else{
				message("申请撤销修改成功");
			}
			$('#revokeDialog').dialog('close');
		}
	});
}

/**
 * 保存当前环节，进入下一环节
 **/
function handle(){
	if(caseData.caseStep==caseData.nextCaseStep){
		message("案件已经结案");
		return;
	}
	$.messager.confirm('确认','您确认想要进入下一环节吗？',function(r){    
	    if (r){    
	    	nextCaseStep_ = $("#accordion div[sort='" + nextsort_ + "']").attr("linkName");
	    	if(online){
	    		/**
	    		 *在线
	    		 **/
	    		nextFlow(caseBasicInfoId,nextCaseStep,nextCaseStep_,nextRequired,userId,userName,getNextCaseStep);
	    	}else{
	    		/**
	    		 *离线
	    		 **/
	    		updateCaseBasicInfoCaseeStepOffLine(caseBasicInfoId,nextCaseStep,nextCaseStep_,nextRequired,getNextCaseStep);
	    	}
	}});  
}

function upLoadDoc(){
	var docId = $("#docNames #id").val();
	var docNameId_=$("#accordion .li-selected").find('span').eq(0).attr("docNameId");
	if($.trim(docId).length>0 || docNameId_ == "1001000500000000"){
		upLoadDocAnnex(docNameId_);
	}else{
		message("请保存当前文书后再上传附件");
	}
	
	if(docNameId_ == "1001000300000000"){
		
	}
}

function upLoadDocAnnex(docNameId){
	var content="<div style='margin:30px'><ul id='doclocalAndCaptureUpload'><li style='width:100px;height:40px;list-style:none;font-size:20px;'><a href='#' onclick='localUpload(this);' id="+docNameId+">本地上传</a></li><li style='width:100px;height:40px;list-style:none;font-size:20px;'><a href='#' onclick='captureUpload(this);' id="+docNameId+">拍照上传</a></li></ul></div>";
	$('#docUpLoadDialog').dialog({
		title: '上传附件',
		width: 300,
		height: 180,
		inline:false,
		closed: false,
		cache: false,
		modal: true,
		content:content
      });
	$('#docUpLoadDialog').dialog("center");
	
/*	var url = ctx + '/jsp/case/upload/fileupload.html?id='+caseBasicInfoId+'&note='+docId;
	if(true){
		var content = '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="yes"></iframe>';
	    var win = $('#docUpLoadDialog').dialog({
	        content: content,
	        title: '上传附件',
		    width: 780,
		    height: 380,
		    left:300,
		    top:220,
	        modal: true,
	        cache: false
	    });

	    win.dialog('open').dialog('refresh');
	}*/
}

function localUpload(obj){
	var url = ctx + '/jsp/case/upload/fileupload.html?id='+caseBasicInfoId+'&note='+obj.id;
	var content = '<iframe src="' + url + '" width="100%" height="99%" frameborder="0" scrolling="yes"></iframe>';
    var win = $('#docLocalUpLoadDialog').dialog({
        content: content,
        title: '上传附件',
	    width: 780,
	    height: 430,
	    left:300,
	    top:220,
        modal: true,
        cache: false,
        buttons:[{
			text:'关闭',
			handler:function(){
				 $('#docLocalUpLoadDialog').dialog('close');
			}
		}]
    });

    win.dialog('open').dialog('refresh');
}
function captureUpload(obj){
//	var id = caseBasicInfoId + "," + obj.id;
	var url = 'gpy.photo://uploadPath=http://'+document.location.host+ctx+'/uploadPhoto'+'&contractName=gpyphotorequestcontract&fid='+caseBasicInfoId+'&note='+obj.id;
    //$('#doclocalAndCaptureUpload').find('li a').eq(1).attr('href',url);
	var a = window.CallCSharpMethod("ShowMessage", url);
    if(a=="true"){
    	message("附件上传成功");
    }
}
/**
 * 获得下一环节
 **/
function getNextCaseStep(){
	caseData.linkNameId=nextLinkNameId;
	caseData.linkName=nextCaseStep;

	//caseStep = $("#accordion #" + nextLinkNameId).attr("linkName");
	var item = {linkNameId:nextLinkNameId,linkName:nextCaseStep};
	initTooltip(item);
	
	$("#accordion div[linkName='" + nextCaseStep_ + "']").each(function(index,item){
		nextsort = parseInt($(item).attr("sort"));
		if(nextsort < caselength-1){
			nextsort_ = nextsort + 1;
		}else{
			nextsort_ = nextsort;
		}
		nextLinkNameId = $("#accordion div[sort='"+nextsort+"']").attr("id");
		nextCaseStep = nextCaseStep_;
		nextRequired = $("#accordion div[sort='"+nextsort+"']").attr('rqed');
		//$('#nextFlow_text').text($("#accordion div[sort='"+nextsort+"']").attr("linkName"));
	});
	
	$("#accordion").accordion('select',$("#accordion div[sort="+nextsort+"]").attr('linkName'));
	$("#accordion div[sort="+nextsort+"] span:first").eq(0).trigger('click');
	//linkNameId=$("#accordion div[sort="+nextsort+"]").attr('id');
	/*
	 * 获得下一环节文书类型
	 
	var templateId=$("#wsmb").combobox('getValue');
	if($.trim(templateId).length<=0){
		message("请先选择文书模板");
		return;
	}else{
	  findAllTemplateDetailByTemplateIdOnLine(templateId,caseBasicInfoId,initCaseeTemplateDetail,"#wslx");
	}*/
}

/**
 * 保存流程
 */
function nextFlow(id,nextCaseStep,nextCaseStep_,nextRequired,userId,userName,callback){
	$.ajax({
		type : "POST",
		url : getContextPath() + "/caseBasicInfo/nextFlow",
		dataType : "json",
		data : {
			id:id,nextCaseStep:nextCaseStep,nextCaseStep_:nextCaseStep_,required:nextRequired,userId:userId,username:userName
		},
		success : function(data) {
		    if(data.result=='success'){
		    	message("案件环节修改成功");
	    		caseData.caseStep=nextCaseStep;
	    		caseData.nextCaseStep=nextCaseStep_;
		    	if(typeof(callback) == 'function'){
		    		callback();
		    	}
		    }else if(data.result=='fail'){
		    	message(data.errorCode + ":" +data.errorMessage);
		    }
		}
	 });
}











function removeTab(){
    var tab = $('#caseInfo').tabs('getSelected');
    if (tab){
        var index = $('#caseInfo').tabs('getTabIndex', tab);
        $('#caseInfo').tabs('close', index);
    }
}

/**
 * 初始化案件办理
 */
function initCaseLinkName(data){
	caselength = data.length;
	$.each(data, function(index, item) {
	/*	$("#ajhjul").append(
				"<div id='" + item.linkNameId + "' sort='" + index + "' caseStep='"+item.linkName+"' " +
					  "style='height:50px;text-align:center'>"+item.linkName).append("</div>");*/
		
		if(index!=caselength-1){
			$("#ajhjul").append('<div id="' + item.linkNameId + '" sort="' + index + '" caseStep="'+item.linkName+'"> <a href="#"><li class="ajzx-l-anniu">'+item.linkName+'</li></a><li class="ajzx-l-down"></li></div>');
		}else{
			$("#ajhjul").append('<div id="' + item.linkNameId + '" sort="' + index + '" caseStep="'+item.linkName+'"> <a href="#"><li class="ajzx-l-anniu">'+item.linkName+'</li></a></div>');
		}
		initTooltip(item);
		
		$("#"+item.linkNameId).bind("click",function(){
			var selected = $(this).attr("selected");
			if(selected == 'selected'){
				return;
			}
			$("#ajhjul div[id!=" + item.linkNameId+"]").removeAttr("selected");
			$(this).attr("selected","selected");
			/**
			 * 取得点击的案件环节
			 */
			sort_click = parseInt($(this).attr("sort"));
			if(nextsort < sort_click){
				message("请先办理上一环节");
				return;
			}
			
			findDocByLinkNameId(item.linkNameId,organId,setDocByOrganIdAndLinkName);
		});
	});
	
	/**
	 * 案件当前环节
	 */
	$("div[caseStep='" + caseStep + "']").each(function(index_,item_){
		sort = parseInt($(item_).attr("sort"));
		if(sort + 1 < caselength){
			nextsort = sort + 1;
			linkNameId = $(this).siblings("div[sort='"+nextsort+"']").attr("id");
			$('#nextFlow_text').text($(this).siblings("div[sort='"+nextsort+"']").attr("caseStep"));
		}else{
			nextsort = sort;
			linkNameId = $(item_).attr("id");
			$('#nextFlow_text').text($(item_).attr("caseStep"));
		}
		
		sort_click = nextsort;
	});
	
	/**
	 * 初始化显示当前环节
	 */
	$("#"+linkNameId).attr("selected","selected");
	findDocByLinkNameId(linkNameId,organId,setDocByOrganIdAndLinkName);
	
}

/**
 * 根据机构ID与环节名称显示办理区域
 */
function setDocByOrganIdAndLinkName(data){
	var tabLength = $("#caseInfo").tabs().tabs('tabs').length;
	for(var i=0;i<=tabLength;i++){
		removeTab();
	}
	$("#hjws").combobox({
		valueField:'docNameId',
        textField:'docName',
        width:200,
        data:data,
		height:26,
		value:data[0].docNameId,
		editable:false,
		onSelect:function(item){
			$('#caseInfo').tabs('select', item.docName);
            if($("#"+item.docNameId > "childs").length == 0){
            	updateTabs(item);
            }
		}
	});
	$.each(data,function(index,item){
		var docPathLen = item.docPath.length;
		if(docPathLen > 0){
			$('#caseInfo').tabs('add',{
				id:item.docNameId,
		        title: item.docName
		    });
			
			$("#"+item.docNameId).panel('options').tab.unbind().bind('click',{index:index},function(e){
	            $('#caseInfo').tabs('select', e.data.index);
	            if($("#"+item.docNameId > "childs").length == 0){
	            	updateTabs(item);
	            }
	        });
		}
	});
	if(data.length != 0){
		$('#caseInfo').tabs('select', 0);
		updateTabs(data[0]);
	}
	$('#caseInfo').tabs('select', 0);
	var height = 43 + nextsort*80; 
	$(".pointer").attr('style','margin-top:' + height + 'px');
}

/**
 *  查询文书模板
 * @param docId
 * @param docPath
 * @param callback
 * @param $con
 */
function queryTemplateByDocId(docId,docPath,callback,$con){
	if($.trim(docId).length<=0){
		return;
	}else{
		 $.ajax({
				type : "POST",
				url : getContextPath() + "/template/queryTemplateByDocId",
				dataType : "json",
				data : {
					docId:docId,
					organId:organId,
					caseBasicInfoId:caseBasicInfoId
				},
				error : function(request) {
					message("获取模板名称失败");
				},
				success : function(data) {
				if(data.result == 'success'){
					if (typeof(callback) == 'function'){
						callback(data.content,docPath,docId,$con);
					}
				  }else{
						message(data.errorCode + ":" +data.errorMessage);
					}
				}
			 });
	}
}

/**
 * 查询文书模板明细
 * @param docId
 * @param docPath
 * @param callback
 * @param $con
 */
function queryTemplateDetailByDocId(docId,docPath,callback,$con){
	if($.trim(docId).length<=0){
		return;
	}else{
		 $.ajax({
				type : "POST",
				url : getContextPath() + "/template/queryTemplateDetailByDocId",
				dataType : "json",
				data : {
					docId:docId,
					organId:organId,
					caseBasicInfoId:caseBasicInfoId
				},
				error : function(request) {
					message("获取化文书类型失败");
				},
				success : function(data) {
				if(data.result == 'success'){
					if (typeof(callback) == 'function'){
						callback(data.content,docPath,docId,$con);
					}
				  }else{
						message(data.errorCode + ":" +data.errorMessage);
					}
				}
			 });
	}
}


/**
 * 根据模板ID加载文书类型
 * @param templateId
 * @param callback
 * @param $con
 *//*
function findAllTemplateDetailByTemplateIdOnLine(templateId,caseBasicInfoId,callback,$con){
	if($.trim(organId).length<=0){
		message("初始化文书类型异常");
		return;
	}else{
	   $.ajax({
			type : "POST",
			url : getContextPath() + "/template/queryTemplateDetails",
			dataType : "json",
			data : {
				templateId:templateId,
				caseBasicInfoId:caseBasicInfoId
			},
			error : function(request) {
				message("获取化文书类型失败");
			},
			success : function(data) {
			if(data.result == 'success'){
				if (typeof(callback) == 'function'){
					callback(data.content,$con);
				}
			  }else{
					message(data.errorCode + ":" +data.errorMessage);
				}
			}
		 });
	}
}*/

/**
 * 设置文书类型
 */
function setCaseeTemplateDetail(data,docPath,docId,$con){
	if(data ==null || data.length<=0){
		$($con).combobox({width:150,editable:false,readonly:true});
		return;
	}
	$($con).combobox({
		valueField: 'id',
		textField: 'templateName',
		width:150,
		editable:false,
		data:data,
		readonly:false,
		onSelect:function(item){
			if(docPath=="sampleEvidence"){
				loadWSTemplate(item,docPath,docId,setSampleEvidenceInputData);
			}
			if(docPath=="entrustIdentify"){
				loadWSTemplate(item,docPath,docId,setEntrustIdentifyInputData);
			}
			if(docPath=="evidenceList"){
				loadWSTemplate(item,docPath,docId,setEvidenceListInputData);
			}
			if(docPath=="caseProcessIdea"){
				loadWSTemplate(item,docPath,docId,setCaseProcessIdeaInputData);
			}
			if(docPath=="documentReceipt"){
				loadWSTemplate(item,docPath,docId,setDocumentReceiptInputData);
			}
			if(docPath=="inquiryNotes"){
				loadWSTemplate(item,docPath,docId,initInquiryNotesNextData);
			}
			else{
				loadWSTemplate(item,docPath,docId,setData);
			}
		}
	});
	
}

function loadWSTemplate(item,docPath,docId,callback){
	 $.ajax({
			type : "POST",
			url : getContextPath() + "/template/loadTemplateDetail",
			dataType : "json",
			data : {
				id:item.id,
				caseBasicInfoId:caseBasicInfoId,
				docId:docId
			},
			error : function(request) {
				message("加载文书模板失败");
			},
			success : function(data) {
			if(data.result == 'success'){
				if (typeof(callback) == 'function'){
					if(docPath=="sampleEvidence"){
						callback(data.content[0],"#"+docPath+"Form",data.content[1]);
					}
					if(docPath=="entrustIdentify"){
						callback(data.content[0],"#"+docPath+"Form",data.content[1]);
					}
					if(docPath=="evidenceList"){
						callback(data.content[0],"#"+docPath+"Form",data.content[1]);
					}
					if(docPath=="caseProcessIdea"){
						callback(data.content[0],"#"+docPath+"Form",data.content[1]);
					}
					if(docPath=="documentReceipt"){
						callback(data.content[0],"#"+docPath+"Form",data.content[1]);
					}
					else{
						callback(data.content[0],"#"+docPath+"Form");
					}
					
				}
			  }else{
					message(data.errorCode + ":" +data.errorMessage);
				}
			}
		 });
}